<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home/index/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item :to="{ path: '/testPaper' }">试卷管理</el-breadcrumb-item>
        <el-breadcrumb-item><a href="/testPaper/addTestPaper">添加试卷</a></el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <el-skeleton :rows="5" :loading="loading" animated>
      <div class="operate">
        <div class="title">
          <strong>添加试卷</strong>
        </div>
        <el-scrollbar height="650px">
          <div v-loading="VLoading" class="TestPagePaperbtn">
            <el-tabs type="border-card">
              <el-tab-pane label="人工组卷">
                <el-descriptions class="margin-top" title="人工组卷" :column="3" border>
                  <template #extra>
                    <el-button @click="addTestPaper" type="primary">添加</el-button>
                  </template>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">试卷名</div>
                    </template>
                    <el-input v-model="AtestPaper.name" placeholder="请输入试卷名" />
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">试卷标签</div>
                    </template>
                    <el-select v-model="AtestPaper.TPLabels" placeholder="标签 ">
                      <el-option label=".NET" value=".NET" />
                      <el-option label="JAVA" value="JAVA" />
                    </el-select>
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">试卷类型</div>
                    </template>
                    <el-select v-model="AtestPaper.TPType" placeholder="类型">
                      <el-option label="普通试卷" value="1" />
                      <el-option label="模拟试卷" value="2" />
                      <el-option label="培训班试卷" value="3" />
                    </el-select>
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">描述</div>
                    </template>
                    <el-input v-model="AtestPaper.TPexplain" type="textarea" />
                  </el-descriptions-item>
                </el-descriptions>
                <div>
                  <div style="padding: 10px">
                    <el-tag>总分：{{ AtestPaper.points }}</el-tag>
                  </div>
                  <div style="padding: 10px">
                    <el-tag>题目数量：{{ AtestPaper.count }}</el-tag>
                  </div>
                  <div style="padding: 10px">
                    <el-tag>等级：{{ AtestPaper.TPLv }}</el-tag>
                  </div>
                </div>
                <div class="bankname">请输入试题名：</div>
                <div class="input">
                  <el-input v-model="AtestPaper.key" class="w-50 m-2" placeholder="请输入想要查找的试题名" @change="search" />
                </div>
                <div style="padding: 30px">
                  <el-table :data="tableData" @selection-change="change" border style="width: 100%">
                    <el-table-column type="selection" width="55" />
                    <el-table-column prop="qcontent" label="题干" width="280" />
                    <el-table-column type="expand">
                      <template #default="props">
                        <el-table :data="props.row.opiton">
                          <el-table-column label="选项" prop="ocontent" />
                          <el-table-column label="是否正确">
                            <template #default="scope">
                              <el-tag :type="
                                scope.row.oisRight == 0 ? 'danger' : 'primary'
                              " disable-transitions>
                                {{ scope.row.oisRight }}
                              </el-tag>
                            </template>
                          </el-table-column>
                        </el-table>
                      </template>
                    </el-table-column>
                    <el-table-column prop="qexplain" label="描述" width="280" />
                    <el-table-column label="分数" width="180">
                      <template #default="scope">
                        <el-input-number v-model="scope.row.grade" :min="1" :max="100" />
                      </template>
                    </el-table-column>
                    <el-table-column label="等级">
                      <template #default="scope">
                        <el-tag disable-transitions>
                          {{ scope.row.qlv }}
                        </el-tag>
                      </template>
                    </el-table-column>
                  </el-table>
                  <div class="paging">
                    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                      :page-sizes="[5, 10, 50, 100]" :page-size="pages.limit" :total="pages.total">
                    </el-pagination>
                  </div>
                </div>
              </el-tab-pane>
              <el-tab-pane label="随机组卷">
                <div>
                  <el-descriptions class="margin-top" title="随机组卷" :column="3" border>
                    <template #extra>
                      <el-button @click="generate" type="primary">生成试卷</el-button>
                    </template>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">数量</div>
                      </template>
                      <el-input-number :disabled="yes" v-model="testPaper.count" :min="1" :max="1000" />
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">试卷最小难度</div>
                      </template>
                      <el-input-number :disabled="yes" v-model="testPaper.minlv" :min="1" :max="10000" />
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">试卷最大难度</div>
                      </template>
                      <el-input-number :disabled="yes" v-model="testPaper.maxlv" :min="1" :max="10000" />
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">试卷名</div>
                      </template>
                      <el-input v-model="testPaper.name" :disabled="no" placeholder="请试输入试卷名" />
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">试卷标签</div>
                      </template>
                      <el-select :disabled="no" v-model="testPaper.lable" placeholder="标签">
                        <el-option label=".NET" value=".NET" />
                        <el-option label="JAVA" value="JAVA" />
                      </el-select>
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">总分</div>
                      </template>
                      <el-input-number :disabled="no" v-model="testPaper.points" :min="1" :max="1000" />
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">试卷类型</div>
                      </template>
                      <el-select :disabled="no" v-model="testPaper.TPType" placeholder="类型">
                        <el-option label="普通试卷" value="1" />
                        <el-option label="模拟试卷" value="2" />
                        <el-option label="培训班试卷" value="3" />
                      </el-select>
                    </el-descriptions-item>
                    <el-descriptions-item>
                      <template #label>
                        <div class="cell-item">试卷描述</div>
                      </template>
                      <el-input v-model="testPaper.TPexplain" type="textarea" />
                    </el-descriptions-item>
                  </el-descriptions>
                </div>
                <div v-if="isShow == 1" style="padding: 20px">
                  <el-table border :data="randomtableData" @selection-change="rchange" style="width: 100%">
                    <el-table-column type="selection" width="55" />
                    <el-table-column prop="qcontent" label="试题名" />
                    <el-table-column type="expand">
                      <template #default="props">
                        <el-table :data="props.row.opiton">
                          <el-table-column label="选项" prop="ocontent" />
                          <el-table-column label="是否正确">
                            <template #default="scope">
                              <el-tag :type="
                                scope.row.oisRight == 0 ? 'danger' : 'primary'
                              " disable-transitions>
                                {{ scope.row.oisRight }}
                              </el-tag>
                            </template>
                          </el-table-column>
                        </el-table>
                      </template>
                    </el-table-column>
                    <el-table-column label="类型" width="180">
                      <template #default="scope">
                        <el-tag>
                          {{ scope.row.qlable }}
                        </el-tag>
                      </template>
                    </el-table-column>
                    <el-table-column label="分数" width="180">
                      <template #default="scope">
                        <el-input-number v-model="scope.row.grade" :min="1" :max="100" />
                      </template>
                    </el-table-column>
                  </el-table>
                  <div class="Rbtn">
                    <el-button type="primary" plain @click="Rsave">保存</el-button>
                  </div>
                </div>
              </el-tab-pane>
              <!-- <el-tab-pane label="人工+随机组卷"> -->
              <!-- 人工组卷 -->
              <!-- <el-descriptions v-if="ARIsshow == 1" class="margin-top" title="人工+随机组卷" :column="2" border>
                  <template #extra>
                    <el-button v-if="ARIsshow == 1" @click="ARAdd" type="primary">保存</el-button>
                  </template>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">试卷名</div>
                    </template>
                    <el-input v-model="ARtestPaper.name" placeholder="Please input" />
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">试卷标签</div>
                    </template>
                    <el-select v-model="ARtestPaper.TPLabels" placeholder="标签 ">
                      <el-option label=".NET" value="shanghai" />
                      <el-option label="JAVA" value="beijing" />
                    </el-select>
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">总条数</div>
                    </template>
                    <el-input-number v-model="ARtestPaper.Zcount" :min="1" :max="1000" />
                  </el-descriptions-item>
                </el-descriptions>
                <div v-if="ARIsshow == 1">
                  <div style="padding: 10px">
                    <el-tag>总分：{{ ARtestPaper.points }}</el-tag>
                  </div>
                  <div style="padding: 10px">
                    <el-tag>人工题目数量：{{ ARtestPaper.Acount }}</el-tag>
                  </div>
                  <div style="padding: 10px">
                    <el-tag>等级：{{ ARtestPaper.TPLv }}</el-tag>
                  </div>
                </div>
                <div v-if="ARIsshow == 1" style="padding: 30px">
                  <el-table :data="ARtableData" @selection-change="changes" border style="width: 100%">
                    <el-table-column type="selection" width="55" />
                    <el-table-column prop="QContent" label="题干" width="280" />
                    <el-table-column type="expand">
                      <template #default="props">
                        <el-table :data="props.row.chliren">
                          <el-table-column label="选项" prop="option" />
                          <el-table-column label="是否正确">
                            <template #default="scope">
                              <el-tag :type="
                                scope.row.IsRigh == 0 ? 'danger' : 'primary'
                              " disable-transitions>
                                {{ scope.row.IsRigh }}
                              </el-tag>
                            </template>
                          </el-table-column>
                        </el-table>
                      </template>
                    </el-table-column>
                    <el-table-column prop="QExplain" label="描述" width="280" />
                    <el-table-column label="分数" width="180">
                      <template #default="scope">
                        <el-input-number v-model="scope.row.grade" :min="1" :max="100" />
                      </template>
                    </el-table-column>
                    <el-table-column label="等级">
                      <template #default="scope">
                        <el-tag disable-transitions>
                          {{ scope.row.QLv }}
                        </el-tag>
                      </template>
                    </el-table-column>
                    <el-table-column label="发布">
                      <template #default="scope">
                        <el-tag :type="
                          scope.row.isPublish === 0 ? 'warning' : 'success'
                        " disable-transitions>{{ scope.row.isPublish === 0 ? "否" : "是" }}</el-tag>
                      </template>
                    </el-table-column>
                    <el-table-column label="禁用">
                      <template #default="scope">
                        <el-tag :type="scope.row.isBan === 0 ? 'success' : 'warning'" disable-transitions>
                          {{ scope.row.isBan === 0 ? "否" : "是" }}
                        </el-tag>
                      </template>
                    </el-table-column>
                    <el-table-column label="使用">
                      <template #default="scope">
                        <el-tag disable-transitions>
                          {{ scope.row.isUse }}
                        </el-tag>
                      </template>
                    </el-table-column>
                  </el-table>
                  <div style="padding: 20px">
                    <el-pagination background layout="prev, pager, next" :total="1000" />
                  </div>
                </div> -->
              <!-- 人工组卷 -->

              <!-- 随机组卷 -->
              <!-- <el-descriptions v-if="ARIsshow == 0" class="margin-top" title="人工+随机组卷" :column="2" border>
                  <template #extra>
                    <el-button v-if="ARIsshow == 0" @click="ARShows" type="primary">生成试卷</el-button>
                  </template>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">试卷名</div>
                    </template>
                    <el-input v-model="ARtestPaper.name" placeholder="Please input" />
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">试卷标签</div>
                    </template>
                    <el-select v-model="ARtestPaper.TPLabels" placeholder="标签 ">
                      <el-option label=".NET" value="shanghai" />
                      <el-option label="JAVA" value="beijing" />
                    </el-select>
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">总条数</div>
                    </template>
                    <el-input-number v-model="ARtestPaper.Zcount" :min="1" :max="1000" />
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">总分(请输入试卷总分)</div>
                    </template>
                    <el-input-number v-model="ARtestPaper.Zgrade" :min="1" :max="1000" />
                  </el-descriptions-item>
                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">最小难度</div>
                    </template>
                    <el-input-number v-model="ARtestPaper.minlv" :min="1" :max="1000" />
                  </el-descriptions-item>

                  <el-descriptions-item>
                    <template #label>
                      <div class="cell-item">最大难度</div>
                    </template>
                    <el-input-number v-model="ARtestPaper.maxlv" :min="1" :max="1000" />
                  </el-descriptions-item>
                </el-descriptions> -->
              <!-- 随机组卷 -->

              <!-- <div v-if="ARShow == 1" class="collapse">
                  <el-collapse v-for="(item, index) in ARquestions" :key="item.index">
                    <el-tag>{{ item.label }}</el-tag>
                    <el-collapse :accordion="true">
                      <el-collapse-item :title="index + 1 + '.' + item.name" :name="item.id">
                        <el-collapse accordion v-for="(item2, index) in item.chilren" :key="item2.index">
                          <div>
                            <span>{{ index + 1 + " . " + item2.option }}</span>
                          </div>
                        </el-collapse>
                      </el-collapse-item>
                    </el-collapse>
                  </el-collapse>
                  <div>
                    <el-button type="primary" class="lastsave" plain @click="lastsave">保存</el-button>
                  </div>
                </div>
              </el-tab-pane> -->
            </el-tabs>
          </div>
        </el-scrollbar>
      </div>
    </el-skeleton>
  </div>
</template>
<script lang="ts">
import { defineComponent, onMounted, reactive, toRefs } from "vue";
import { AddTestPaper } from "@/utils/Exam/testPaper/addTestPaper";
import { ElMessage, ElMessageBox } from "element-plus";
import { TaskPaper } from '@/api/Product/exaam';
import router from "@/router";

var _exam = new TaskPaper();

export default defineComponent({
  setup() {
    const data = reactive<AddTestPaper>({
      // 人工
      AtestPaper: {
        name: "",
        points: 0,
        count: 0,
        TPLv: 0,
        TPLabels: "",
        TPType: null,
        TPexplain: "",
        key: ""
      },
      //人工
      tableData: [],
      //随机
      testPaper: {
        name: "",
        lable: "",
        count: 0,
        points: 0,
        minlv: 0,
        maxlv: 0,
        TPexplain: "",
        TPType: ""
      },
      isShow: 0,
      //随机
      randomtableData: [],
      //人工+随机
      ARtestPaper: {
        name: "",
        TPLabels: "",
        Acount: 0, //人工数量
        Rcount: 0, //随机数量
        Zcount: 0, //总条数
        Zgrade: 0,
        TPLv: 0,
        points: 0,
        minlv: 0,
        maxlv: 0,
      },
      //人工+随机
      ARtableData: [],
      //人工+随机
      ARquestions: [],
      ARIsshow: 1,
      //人工+随机生成
      ARShow: 0,
      array: [],
      num: [],
      sum: 0,
      grade: 0,
      pages: {
        page: 1,
        limit: 10,
        total: 10,
      },
      loading: true,
      VLoading: false,
      no: true,
      yes: false,
      questions: [],
      rquestions: [],
      rlv: []
    });
    const methods = {
      //人工增加标签
      change(val: any) {
        data.questions = [];
        val.forEach((e: any) => {
          data.questions.push({
            qid: e.qid,
            qscore: e.grade
          });
        });
        data.AtestPaper.count = val.length;
        data.array = [];
        data.sum = 0;
        data.AtestPaper.points = 0;
        for (let i = 0; i < val.length; i++) {
          data.array.push(val[i].qlv);
          data.sum += val[i].qlv;
          data.AtestPaper.points += val[i].grade;
        }
        if (data.sum != 0) {
          let num: number = Number((data.sum / data.array.length).toFixed(0))
          data.AtestPaper.TPLv = num;
        } else {
          data.AtestPaper.TPLv = 0;
        }
      },
      rchange(val: any) {
        data.rquestions = [];
        data.rlv = [];
        val.forEach((e: any) => {
          data.rquestions.push({
            qid: e.qid,
            qscore: e.grade
          });
          data.testPaper.points = 0;
          data.rquestions.forEach((res: any) => {
            data.testPaper.points += res.qscore
          })
          data.rlv.push({
            rlv: e.qlv
          })
        });
      },
      //人工+随机增加标签
      changes(val: any) {
        data.ARtestPaper.Acount = val.length;
        data.array = [];
        data.sum = 0;
        data.ARtestPaper.points = 0;
        for (let i = 0; i < val.length; i++) {
          data.array.push(val[i].qlv);
          data.sum += val[i].qlv;
          data.ARtestPaper.points += val[i].grade;
        }
        if (data.sum != 0) {
          let num: number = Number((data.sum / data.array.length).toFixed(0))
          data.ARtestPaper.TPLv = num;
        } else {
          data.ARtestPaper.TPLv = 0;
        }
      },
      GetQuestion() {
        _exam.ArtificialTaskPaper({
          page: data.pages.page,
          limit: data.pages.limit,
          key: data.AtestPaper.key,
        }).then(res => {
          if (res.code === 0) {
            data.loading = false;
            data.tableData = res.data.data
            data.pages.total = res.data.total
          }
        })
      },
      search(val: any) {
        data.AtestPaper.key = val;
        methods.GetQuestion();
      },
      handleSizeChange(val: number) {
        data.pages.limit = val;
        methods.GetQuestion();
      },
      handleCurrentChange(val: number) {
        data.pages.page = val;
        methods.GetQuestion();
      },
      //随机生成试卷
      generate() {
        data.isShow = 1;
        data.no = false;
        data.VLoading = true;
        data.yes = true;
        _exam.ConstituteTaskPaper({
          count: data.testPaper.count,
          minLv: data.testPaper.minlv,
          maxLv: data.testPaper.maxlv
        }).then(res => {
          data.randomtableData = res.data
          data.VLoading = false;
        })
      },
      //随机生成保存
      Rsave() {
        let lv = 0;
        data.rlv.forEach((e: any) => {
          lv += e.rlv
        })
        var nums = Number((lv / data.rlv.length + 1).toFixed(0));
        lv = nums;
        let dto = {
          tpname: data.testPaper.name,
          tplv: lv,
          tplabels: data.testPaper.lable,
          tpcount: data.testPaper.count,
          tptype: parseInt(data.testPaper.TPType.toString()),
          tpsocre: data.testPaper.points,
          tpexplain: data.testPaper.TPexplain,
          tppasssocre: data.testPaper.points * 0.6,
          tpmodel: 2,
          questions: data.rquestions
        };
        let jsonStr = JSON.stringify(dto);
        _exam.AddTaskPaper({ jsonStr: jsonStr }).then((res: any) => {
          if (res.code === 0) {
            ElMessage.success(res.message);
            router.push("/testPaper");
          } else {
            ElMessage.success(res.message);
          }
        });
      },
      //添加人工组卷
      addTestPaper() {
        let dto = {
          tpname: data.AtestPaper.name,
          tplv: data.AtestPaper.TPLv,
          tplabels: data.AtestPaper.TPLabels,
          tpcount: data.AtestPaper.count,
          tptype: parseInt(data.AtestPaper.TPType.toString()),
          tpsocre: data.AtestPaper.points,
          tpexplain: data.AtestPaper.TPexplain,
          tppasssocre: data.AtestPaper.points * 0.6,
          tpmodel: 1,
          questions: data.questions
        };

        let jsonStr = JSON.stringify(dto);
        _exam.AddTaskPaper({ jsonStr: jsonStr }).then((res: any) => {
          if (res.code === 0) {
            ElMessage.success(res.message);
            router.push("/testPaper");
          } else {
            ElMessage.success(res.message);
          }
        });
      },
    };
    onMounted(() => {
      methods.GetQuestion();
    })
    return {
      ...toRefs(data),
      ...methods,
    };
  },
});
</script>
<style scoped>
.crumbs {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  padding: 20px;
  margin: 15px;
  background-color: white;
  width: 94%;
}

.operate {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
  padding: 10px;
  margin: 10px;
  background-color: white;
  width: 94%;
}

.title {
  padding: 10px;
  font-size: 20px;
}

.testfrom {
  width: 60%;
}

.Rbtn {
  padding: 20px;
  float: right;
}

.collapse {
  padding: 20px;
}

.lastsave {
  margin: 20px;
  float: right;
}

.input {
  margin-left: 10px;
  width: 300px;
  display: inline-block;
}

.bankname {
  display: inline-block;
  margin-left: 20px;
  padding: 5px;
}
</style>
